package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.config.security.SecurityUtils;
import com.neuedu.yyzx.dto.CustomerNurseSetDTO;
import com.neuedu.yyzx.dto.NurseRecordDTO;
import com.neuedu.yyzx.po.*;
import com.neuedu.yyzx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/nurserecord")
public class NurseRecordController {
    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private NurserecordService nurserecordService;
    @Autowired
    private NursecontentService nursecontentService;
    @Autowired
    private CustomernurseitemService customernurseitemService;

    @GetMapping("/list")
    public Ret list() {
        return Ret.ok(nurserecordService.list());
    }

    @GetMapping("/customerListSelf")
    public Ret<List<CustomerNurseSetDTO>> customerListSelf(Page<CustomerNurseSetDTO> page, Customer customer) {
        SecurityUtils securityUtils = new SecurityUtils(userService);
        User nowUser = securityUtils.getUserInfo();
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        if (customer.getCustomerName() != null && !customer.getCustomerName().trim().isEmpty()) {
            String trimmedName = customer.getCustomerName().trim();
            queryWrapper.like(Customer::getCustomerName, trimmedName);
        }
//        queryWrapper.eq(Customer::getUserId, nowUser.getId());
        page = customerService.pageWithDetails(page, queryWrapper);
        return Ret.ok(page);
    }

    @GetMapping("/recordListSelf")
    public Ret<List<NurseRecordDTO>> recordListSelf(Page<NurseRecordDTO> page, Customer customer) {
        SecurityUtils securityUtils = new SecurityUtils(userService);
        User nowUser = securityUtils.getUserInfo();
        String searchName = customer.getCustomerName();
        if (searchName != null && !searchName.trim().isEmpty()) {
            String trimmedName = searchName.trim();
            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(Customer::getCustomerName, trimmedName);
            List<Integer> customerIds = customerService.list(queryWrapper).stream()
                    .map(Customer::getCustomerId)
                    .collect(Collectors.toList());
            LambdaQueryWrapper<Nurserecord> queryWrapper1 = buildQueryWrapper(customerIds); // 封装查询条件构建逻辑
            if (!customerIds.isEmpty()) {
                System.out.println(queryWrapper1.getSqlSelect());
            } else {
                return Ret.ok(new Page<>());
            }
            page = nurserecordService.pageDetails(page, queryWrapper1);
            return Ret.ok(page);
        } else {
            LambdaQueryWrapper<Nurserecord> queryWrapper = new LambdaQueryWrapper<>();

            page = nurserecordService.pageDetails(page, queryWrapper);
            return Ret.ok(page);
        }
    }

    // 新增方法：封装查询条件构建逻辑
    private LambdaQueryWrapper<Nurserecord> buildQueryWrapper(List<Integer> customerIds) {
        LambdaQueryWrapper<Nurserecord> queryWrapper = new LambdaQueryWrapper<>();
        if (!customerIds.isEmpty()) {
            queryWrapper.in(Nurserecord::getCustomerId, customerIds); // 明确指定表别名 c
        }
        return queryWrapper;
    }

    @GetMapping("/itempagebyid")
    public Ret<List<Customernurseitem>> itemPageById(Page<Customernurseitem> page) {
//        SecurityUtils securityUtils = new SecurityUtils();
//        User nowUser = securityUtils.getUserInfo();
//        if (nowUser.getId() != null) {
//            LambdaQueryWrapper<Customernurseitem> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(Customernurseitem::getCustormerId, nowUser.getId());
//            page = customernurseitemService.page(page, wrapper);
//        }

        return Ret.ok(page);
    }

    @GetMapping("/getById/{id}")
    public Ret<NurseRecordDTO> getById(@PathVariable int id) {
        Nurserecord nurserecord = nurserecordService.getById(id);
        Integer customerId = nurserecord.getCustomerId();
        Integer itemId = nurserecord.getItemId();
        Integer userId = nurserecord.getUserId();
        Customer customer = customerService.getById(customerId);
        Nursecontent nursecontent = nursecontentService.getById(itemId);
        User user = userService.getById(userId);
        NurseRecordDTO nurseRecordDTO = new NurseRecordDTO();
        nurseRecordDTO.setId(nurserecord.getId());
        nurseRecordDTO.setCustomerId(nurserecord.getCustomerId());
        nurseRecordDTO.setItemId(nurserecord.getItemId());
        nurseRecordDTO.setUserId(nurserecord.getUserId());
        nurseRecordDTO.setNursingContent(nurserecord.getNursingContent());
        nurseRecordDTO.setNursingTime(nurserecord.getNursingTime());
        nurseRecordDTO.setNursingCount(nurserecord.getNursingCount());
        nurseRecordDTO.setCustomerName(customer.getCustomerName());
        nurseRecordDTO.setNursingName(nursecontent.getNursingName());
        nurseRecordDTO.setNickName(user.getNickname());
        return Ret.ok(nurseRecordDTO);
    }

    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Nurserecord nurserecord) {
        boolean result = nurserecordService.saveOrUpdate(nurserecord);
        return Ret.ok(result);
    }

    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        boolean result = nurserecordService.removeById(id);
        return Ret.ok(result);
    }
}
